package org.darcy.youchat.library.emoji;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 贴图适配器
 */
public class StickerAdapter extends BaseAdapter {

    private Context mContext;
    private StickerCategory mCategory;
    private int startIndex;

    public StickerAdapter(Context mContext, StickerCategory category, int startIndex) {
        this.mContext = mContext;
        this.mCategory = category;
        this.startIndex = startIndex;
    }


    @Override
    public int getCount() {
        int count = mCategory.getStickers().size() - startIndex;
        count = Math.min(count, 8);
        return count;
    }

    @Override
    public Object getItem(int position) {
        return mCategory.getStickers().get(startIndex + position);
    }

    @Override
    public long getItemId(int position) {
        return startIndex + position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        StickerViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(mContext, R.layout.nim_sticker_picker_view, null);
            viewHolder = new StickerViewHolder();
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.sticker_thumb_image);
            viewHolder.descLabel = (TextView) convertView.findViewById(R.id.sticker_desc_label);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (StickerViewHolder) convertView.getTag();
        }
        int index = startIndex + position;
        if (index >= mCategory.getStickers().size())
            return convertView;
        StickerItem sticker = mCategory.getStickers().get(index);
        if (sticker == null) {
            return convertView;
        }
        String stickerBitmapPath = StickerManager.getInstance().getStickerBitmapPath(sticker.getCategory(), sticker.getName());
        YouChatEmotionKit.getImageLoader().loadEmoji(mContext, stickerBitmapPath, viewHolder.imageView);
        viewHolder.descLabel.setVisibility(View.GONE);
        return convertView;
    }

    class StickerViewHolder {
        public ImageView imageView;
        public TextView descLabel;
    }
}
